package com.anysoft.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.GetBankCardListBean;
import com.anysoft.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所绑定的银行卡列表适配器
 * Created by yanbin on 2017-03-28.
 */
public class BindBankCardAdapter extends BaseAdapter {

    private Context context;

    private List<GetBankCardListBean.BankCardInfo> listBank;

    private Map<String, Integer> mapBankLogoResource;

    public BindBankCardAdapter(Activity context, List<GetBankCardListBean.BankCardInfo> listBank) {
        this.context = context;
        this.listBank = listBank;
        mapBankLogoResource = new HashMap<String, Integer>();
        mapBankLogoResource.put("002", R.drawable.logo_bank_002);//中国工商银行
        mapBankLogoResource.put("003", R.drawable.logo_bank_003);//中国农业银行
        mapBankLogoResource.put("004", R.drawable.logo_bank_004);//中国银行
        mapBankLogoResource.put("005", R.drawable.logo_bank_005);//中国建设银行
        mapBankLogoResource.put("006", R.drawable.logo_bank_006);//中国交通银行
        mapBankLogoResource.put("007", R.drawable.logo_bank_007);//招商银行
        mapBankLogoResource.put("009", R.drawable.logo_bank_009);//光大银行
        mapBankLogoResource.put("011", R.drawable.logo_bank_011);//上海浦东发展银行
        mapBankLogoResource.put("013", R.drawable.logo_bank_013);//深圳发展银行
        mapBankLogoResource.put("014", R.drawable.logo_bank_014);//民生银行
        mapBankLogoResource.put("016", R.drawable.logo_bank_016);//广东发展银行
        mapBankLogoResource.put("017", R.drawable.logo_bank_017);//上海银行
        mapBankLogoResource.put("019", R.drawable.logo_bank_019);//上海农村商业银
        mapBankLogoResource.put("020", R.drawable.logo_bank_020);//兴业银行
        mapBankLogoResource.put("934", R.drawable.logo_bank_934);//邮政储蓄银行
        mapBankLogoResource.put("904", R.drawable.logo_bank_904);//南京银行
        mapBankLogoResource.put("905", R.drawable.logo_bank_905);//金华银
        mapBankLogoResource.put("920", R.drawable.logo_bank_920);//平安银行
        mapBankLogoResource.put("932", R.drawable.logo_bank_932);//中信银行
        mapBankLogoResource.put("012", R.drawable.logo_bank_012);//华夏银行
        mapBankLogoResource.put("6", R.drawable.logo_bank_tty);//天天盈
        //3.0.7新增银行卡 start
        mapBankLogoResource.put("938", R.drawable.logo_bank_938);//渤海银行
        mapBankLogoResource.put("008", R.drawable.logo_bank_008);//北京银行
        mapBankLogoResource.put("903", R.drawable.logo_bank_903);//宁波银行
        mapBankLogoResource.put("907", R.drawable.logo_bank_907);//温州银行
        mapBankLogoResource.put("904", R.drawable.logo_bank_904);//南京银行
        mapBankLogoResource.put("905", R.drawable.logo_bank_905);//金华银行
        mapBankLogoResource.put("908", R.drawable.logo_bank_908);//齐商银行
        mapBankLogoResource.put("909", R.drawable.logo_bank_909);//重庆银行
        mapBankLogoResource.put("910", R.drawable.logo_bank_910);//贵阳银行
        mapBankLogoResource.put("912", R.drawable.logo_bank_912);//晋商银行
        mapBankLogoResource.put("913", R.drawable.logo_bank_913);//青岛银行
        mapBankLogoResource.put("901", R.drawable.logo_bank_901);//长沙银行
        mapBankLogoResource.put("914", R.drawable.logo_bank_914);//泉州银行
        mapBankLogoResource.put("902", R.drawable.logo_bank_902);//富滇银行
        mapBankLogoResource.put("902", R.drawable.logo_bank_902);//晋城银行
        mapBankLogoResource.put("917", R.drawable.logo_bank_917);//富滇银行
        mapBankLogoResource.put("919", R.drawable.logo_bank_919);//江苏银行
        mapBankLogoResource.put("923", R.drawable.logo_bank_923);//汇丰银行
        mapBankLogoResource.put("032", R.drawable.logo_bank_032);//恒丰银行
        mapBankLogoResource.put("921", R.drawable.logo_bank_921);//重庆三峡银行
        mapBankLogoResource.put("019", R.drawable.logo_bank_019);//上海农村商业银行
        mapBankLogoResource.put("922", R.drawable.logo_bank_922);//重庆农村商业银行
        mapBankLogoResource.put("947", R.drawable.logo_bank_947);//北京农村商业银行
        mapBankLogoResource.put("924", R.drawable.logo_bank_924);//广州农村商业银行
        //end
//        mapBankLogoResource.put("AAE",R.drawable.logo_bank_AAE);//工行现金宝
        mapBankLogoResource.put("default", R.drawable.logo_bank_default);
    }


    @Override
    public int getCount() {
        if (listBank == null)
            return 0;
        return listBank.size();
    }

    @Override
    public Object getItem(int position) {
        return listBank.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_item_bind_bank_card, null);
            holder = new ViewHolder();
            holder.image_bank_logo = (ImageView) convertView.findViewById(R.id.image_bank_logo);
            holder.tv_bank_name = (TextView) convertView.findViewById(R.id.tv_bank_name);
            holder.tv_bank_type = (TextView) convertView.findViewById(R.id.tv_bank_type);
            holder.tv_bank_card_no = (TextView) convertView.findViewById(R.id.tv_bank_card_no);
            holder.image_bank_channel = (ImageView) convertView.findViewById(R.id.image_bank_channel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        int resourceid = mapBankLogoResource.get("default").intValue();
        if (mapBankLogoResource.containsKey(listBank.get(position).getBankCode())) {
            resourceid = mapBankLogoResource.get(listBank.get(position).getBankCode()).intValue();
        }
        holder.image_bank_logo.setImageResource(resourceid);
        holder.image_bank_channel.setVisibility(View.VISIBLE);
        if ("3".equals(listBank.get(position).getCapitalMode())) {//银联
            holder.image_bank_channel.setImageResource(R.drawable.yinlian);
        } else if ("M".equals(listBank.get(position).getCapitalMode())) {//通连
            holder.image_bank_channel.setImageResource(R.drawable.tonglian);
        } else {//4：直连 没有值的默认是直连
            holder.image_bank_channel.setVisibility(View.INVISIBLE);
        }

        holder.tv_bank_name.setText(listBank.get(position).getBankName());
        String accountNo = listBank.get(position).getBankAccountNo();
        if (!StringUtils.isNullOrEmpty(accountNo)) {
            if (accountNo.length() > 4) {
                accountNo = "**** **** **** " + accountNo.substring(accountNo.length() - 4);
            }
        }
//        String accountNo_tmp="";
//        if(accountNo!=null){
//            int count = accountNo.length()/4;
//            if(accountNo.length()%4==0){
//                count--;
//            }
//            if(count<=0){
//                accountNo_tmp = accountNo;
//            }
//            for(int i=0;i<count;i++){
//                accountNo_tmp += "**** ";
////                accountNo_tmp += accountNo.substring(i*4,i*4+4)+" ";
//                if(i==count-1){
//                    accountNo_tmp += accountNo.substring(i*4+4);
//                }
//            }
////            if(accountNo.length()%4>0){
////                count--;
////            }
//        }
        holder.tv_bank_card_no.setText(accountNo + "");
        return convertView;
    }


    public static class ViewHolder {
        ImageView image_bank_logo;
        TextView tv_bank_name;
        TextView tv_bank_type;
        TextView tv_bank_card_no;
        ImageView image_bank_channel;
    }

}
